<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <div slot="detail">
      <a-form-model ref="form" :model="model" :rules="validatorRules" >
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="促销名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入促销名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否叠加" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isSole">
              <j-dict-select-tag type="radio" v-model="model.isSole" dictCode="shop_promo_is_sole" placeholder="请选择是否叠加" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="分站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="siteId">
              <search-select v-model="model.siteId" placeholder="请选择分站" dict="mk_site_basic,name,id"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="merchantId">
              <a-input v-model="model.merchantId" placeholder="请选择商家"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="门店" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
              <a-input v-model="model.shopId" placeholder="请选择门店" ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="促销类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="shop_promo_type" placeholder="请选择促销类型"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户身份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customer">
              <j-dict-select-tag type="list" v-model="model.customer" dictCode="shop_promo_customer" placeholder="请选择客户身份"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠模式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="model">
              <j-dict-select-tag type="list" v-model="model.model" dictCode="shop_promo_model" placeholder="请选择优惠模式"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="订单金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="target">
              <a-input v-model="model.target" placeholder="请输入订单金额"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="discount">
              <a-input v-model="model.discount" placeholder="请输入优惠金额"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="title!='新增'">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="radio" v-model="model.status" dictCode="shop_promo_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      </div>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ShopPromoForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      title: String,
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           type: [
              { required: true, message: '请输入促销类型!'},
           ],
           name: [
              { required: true, message: '请输入促销名称!'},
           ],
          //  merchantId: [
          //     { required: true, message: '请选择商家!'},
          //  ],
           siteId: [
              { required: true, message: '请选择分站!'},
           ],
           status: [
              { required: true, message: '请选择状态!'},
           ],
           customer: [
              { required: true, message: '请选择客户身份!'},
           ],
           model: [
              { required: true, message: '请选择优惠模式!'},
           ],
           target: [
              { required: true, message: '请输入消费金额!'},
           ],
           discount: [
              { required: true, message: '请输入优惠金额!'},
           ],
           isSole: [
              { required: true, message: '请选择是否叠加!'},
           ],
        },
        url: {
          add: "/shopPromo/add",
          edit: "/shopPromo/edit",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      ...mapGetters([
        "userInfoSite",
        "userInfoMerchant",
        "userInfoCurrentShop",
      ]),
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.$emit('handleChangeThreeElements', {
          site: this.userInfoSite ? this.userInfoSite['name'] : null,
          merchant: this.userInfoMerchant ? this.userInfoMerchant['name'] : null,
          shop: this.userInfoCurrentShop ? this.userInfoCurrentShop['name'] : null
        })
        this.visible = true;
      },
      edit (record) {
        console.log(this.model)
        this.model = Object.assign({}, record);
        this.$emit('handleChangeThreeElements', {
          site: this.model['siteId_dictText'],
          merchant: this.model['merchantId_dictText'],
          shop: this.model['shopId_dictText']
        })
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>